package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(connectionFactory);
        
        // 设置Key的序列化方式（默认就是StringRedisSerializer）
        template.setKeySerializer(RedisSerializer.string());
        
        // 设置Value的序列化方式（默认StringRedisSerializer）
        template.setValueSerializer(RedisSerializer.string());
        
        // 针对Hash类型的数据操作
        template.setHashKeySerializer(RedisSerializer.string());
        template.setHashValueSerializer(RedisSerializer.string());
        
        template.afterPropertiesSet();
        return template;
    }
}
